package com.sysu.minsysu.wdzj;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.minSYSU.db.DatabaseHelper;
import com.minSYSU.db.Records;
import com.sysu.minsysu.R;

public class NewRecord extends Activity {
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");       
	private Date curDate; 
	
	private TextView name, time, content;
	private Spinner location;
	
	private Button add_button, reset_button;
	private DatabaseHelper db = null;
	private Records Record;
	private Intent intent = new Intent();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_record);
		db = new DatabaseHelper(this);
		initViews();
	}
	
	private void initViews() {
		 name = (TextView) findViewById(R.id.name);
		 time = (TextView) findViewById(R.id.time);
		 content = (TextView) findViewById(R.id.content);
		 location = (Spinner) findViewById(R.id.loc);
		 
		 add_button = (Button) findViewById(R.id.add);
		 reset_button = (Button) findViewById(R.id.reset);		 
		 add_button.setOnClickListener(buttonViewClickListener);
		 reset_button.setOnClickListener(buttonViewClickListener);
		 
		 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.site, android.R.layout.simple_spinner_dropdown_item);
		 adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
		 location.setAdapter(adapter);
		 
		 curDate = new Date(System.currentTimeMillis());//获取当前时间     					
		 time.setText(formatter.format(curDate));
		 name.setText("user");
		 intent.setClass(this, MinSYSUMessageActivity.class);
	}
	
	private View.OnClickListener buttonViewClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
				case R.id.add:
					Record = new Records( name.getText().toString(), content.getText().toString(), time.getText().toString(), location.getSelectedItem().toString());
					Log.e("new record", "name:" + name.getText().toString() +
										" content:" + content.getText().toString()+
										" time:" + time.getText().toString()+
										" location:" + location.getSelectedItem().toString());
					db.AddRecords(Record);
					startActivity(intent);
					finish();
					break;
				case R.id.reset:
					name.setText("");
					curDate = new Date(System.currentTimeMillis());//获取当前时间     					
					time.setText(formatter.format(curDate));
					content.setText("");
					break;
			}
			
		}
	};
}
